// cQuickPlaceDB.cpp : implementation file
//

#include "stdafx.h"
#include "qpmig.h"
#include "cQuickPlaceDB.h"
#include "cNotesFolderInfo.h"
#include "cUser.h"
#include "noteshelperfunctions.h"
extern "C" __declspec( dllimport )int GetMime(DBHANDLE hDB, NOTEID NoteID, char *szBuf, int buflen);
extern "C" __declspec( dllimport )int SaveMime(char *szFileName, DBHANDLE hDB, NOTEID NoteID);


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CQPMigApp theApp;

/////////////////////////////////////////////////////////////////////////////
// cQuickPlaceDB

IMPLEMENT_DYNCREATE(cQuickPlaceDB, CObject)

cQuickPlaceDB::cQuickPlaceDB(LPCSTR lpszNotesServerCN,LPCSTR lpszNotesDatabaseCN,LPCSTR lpszName)
{
	strcpy_s(szNotesServerCN,256,lpszNotesServerCN);
	strcpy_s(szNotesDatabaseCN,256,lpszNotesDatabaseCN);
	strName=lpszName;
	bstrDescription=NULL;
	CString cInfo;
	cInfo.Format("This is the QuickPlace %s", lpszName);
	bstrDescription=cInfo.AllocSysString();

}
cQuickPlaceDB::cQuickPlaceDB()
{
	bstrDescription=NULL;

}
cQuickPlaceDB::~cQuickPlaceDB()
{
	int i;
	if(db.IsOpen())
		db.Close();
	for(i=0;i<=cobjNotesFolders.GetUpperBound();i++)
		{
			cNotesFolderInfo *pFolderInfo=(cNotesFolderInfo *)cobjNotesFolders.GetAt(i);
			if(pFolderInfo)
				delete pFolderInfo;
		}
	for(i=0;i<=cobjArUsers.GetUpperBound();i++)
		{
			CUser *pUser=(CUser *)cobjArUsers.GetAt(i);
			if(pUser)
				delete pUser;
		}
	if(bstrDescription)
		::SysFreeString(bstrDescription);

}
bool cQuickPlaceDB::Process()
	{
		theApp.stControl.pDebugLog->LogOneLine("Entering cQuickPlaceDB::Process()");
		theApp.stControl.pDebugLog->Flush();
	

		LNSTATUS Status;
		if(LNNOERROR==theApp.stControl.session.GetDatabase(szNotesDatabaseCN, &db,szNotesServerCN))
			{
				// Open the database
				if(LNNOERROR!=(Status=db.Open(LNDBOPENFLAGS_NO_USER_INFO)))
					{
						char errorBuf[LNERROR_MESSAGE_LENGTH];
						LNGetErrorMessage(Status, errorBuf);
						AfxMessageBox(errorBuf,MB_OK|MB_ICONERROR); 
						theApp.stControl.pDebugLog->LogOneLine(errorBuf);
						return false;
					}
				theApp.stControl.pDebugLog->LogOneLine(szNotesDatabaseCN);
				theApp.stControl.pDebugLog->Flush();


			}
		else
			return false;
		bool bRetValue=true;
		if(db.IsOpen()) //Verify that the db is open
			{
				//Add the default folders Calendar,Tasks
				cNotesFolderInfo *pFolderInfo=new cNotesFolderInfo;
				if(pFolderInfo)
					{
						pFolderInfo->m_strFolderDisplayName="Calendar";
						pFolderInfo->m_strNotesName="h_Calendar";
						pFolderInfo->m_wType=CALENDARFOLDER;
						if(!theApp.stControl.m_bAdvanced)
							pFolderInfo->b_Transfer=TRUE;
						cobjNotesFolders.Add(pFolderInfo);
						
					}
				pFolderInfo=new cNotesFolderInfo;
				if(pFolderInfo)
					{
						pFolderInfo->m_strFolderDisplayName="Quickplace Tasks";
						pFolderInfo->m_strNotesName="h_TaskList";
						pFolderInfo->m_wType=TASKFOLDER;
						if(!theApp.stControl.m_bAdvanced)
							pFolderInfo->b_Transfer=TRUE;
						cobjNotesFolders.Add(pFolderInfo);
					}

				LNViewFolder     folder;
				LNVFNavigator nav;          // Working navigator
				LNVFEntry        entry;
				theApp.stControl.pDebugLog->LogOneLine("Enumerating through the Folder list");
				if(LNNOERROR==db.GetViewFolder("h_Folders", &folder))  // Get list of folders
					{
						if(LNNOERROR==folder.Open())
							{
								folder.GetEntries(&nav);
								folder.GotoFirst(&entry);
								if (folder.GetEntryCount() >0)
									{
										do
											{
												LNDocument	Doc;
												LNString Value;
												LNText oneItem;
												
												if(LNNOERROR==entry.GetDocument(&Doc))
													{
														if(LNNOERROR==Doc.Open())
															{
																LNString Ancestor;
																if(Doc.HasItem("h_Ancestry"))
																{
																	if(LNNOERROR==Doc.GetItem("h_Ancestry",&oneItem))
																	{
																		Ancestor=oneItem[0];
																	}
																	LNUniversalID lnUnid=Doc.GetUniversalID();
																	LNString sUnid=lnUnid.GetText();
																	if(Ancestor!=sUnid)
																	{
																		Doc.Close();
																		continue;
																	}
																}

																if(LNNOERROR==Doc.GetItem("h_Name",&oneItem))
																	{
																		cNotesFolderInfo *pFolderInfo=new cNotesFolderInfo;
																		if(pFolderInfo)
																			{
																				pFolderInfo->m_wType=DOCFOLDER;
																				Value=oneItem[0];
																				pFolderInfo->m_strFolderDisplayName=Value;
																				pFolderInfo->m_strFolderDisplayName.TrimLeft();
																				pFolderInfo->m_strFolderDisplayName.TrimRight();
																				theApp.stControl.pDebugLog->LogOneLine(Value);
																				theApp.stControl.pDebugLog->Flush();
																				if(LNNOERROR==Doc.GetItem("h_FolderStyle",&oneItem))
																					{
																						Value=oneItem[0];
																						if("5"==Value && !pFolderInfo->m_strFolderDisplayName.CompareNoCase("discussion"))
																							pFolderInfo->m_wType=DISCFOLDER;
																						if("h_Calendar"==Value)
																							pFolderInfo->m_wType=CALENDARFOLDER;
																						if("h_Task"==Value)
																							pFolderInfo->m_wType=TASKFOLDER;

																					}


																				if(LNNOERROR==Doc.GetItem("h_SystemName",&oneItem))
																					{
																						Value=oneItem[0];
																						pFolderInfo->m_strNotesName=Value;
																						if(!theApp.stControl.m_bAdvanced)
																							pFolderInfo->b_Transfer=TRUE;
																						cobjNotesFolders.Add(pFolderInfo);

																					}
																				else
																					delete pFolderInfo; //No system name given, hence the item cannot be opened with that name
																			}
																	}
																Doc.Close();
															}
													}
												
											} while (nav.GotoNextMain(&entry) != LNWARN_NOT_FOUND);
									}
								folder.Close();   // Close the folder

							}
					}
				theApp.stControl.pDebugLog->LogOneLine("Enumerating through the Subroom list");
				if(LNNOERROR==db.GetViewFolder("h_AreaView", &folder))  // Get list of folders
					{
						if(LNNOERROR==folder.Open())
							{
								folder.GetEntries(&nav);
								folder.GotoFirst(&entry);
								if (folder.GetEntryCount() >0)
									{
					

										do
											{
												LNDocument	Doc;
												LNString Value;
												LNText oneItem;
												
												if(LNNOERROR==entry.GetDocument(&Doc))
													{
														if(LNNOERROR==Doc.Open())
															{

																if(LNNOERROR==Doc.GetItem("h_Name",&oneItem))
																	{
																		cNotesFolderInfo *pFolderInfo=new cNotesFolderInfo;
																		if(pFolderInfo)
																			{
																				Value=oneItem[0];
																				pFolderInfo->m_strFolderDisplayName=Value;
																				pFolderInfo->m_strFolderDisplayName.TrimLeft();
																				pFolderInfo->m_strFolderDisplayName.TrimRight();
																				CString cLastPart;
																				int nPos=pFolderInfo->m_strFolderDisplayName.ReverseFind('\\');
																				if(nPos>0)
																					cLastPart=pFolderInfo->m_strFolderDisplayName.Right(nPos);
																				else
																					cLastPart=pFolderInfo->m_strFolderDisplayName;

																				theApp.stControl.pDebugLog->LogOneLine(Value);
																				theApp.stControl.pDebugLog->Flush();

																				if(LNNOERROR==Doc.GetItem("h_LocDbName",&oneItem))
																					{
																						Value=oneItem[0];
																						theApp.stControl.pDebugLog->LogOneLine("Subroom DB");
																						theApp.stControl.pDebugLog->LogOneLine(Value);
																						theApp.stControl.pDebugLog->Flush();


																						pFolderInfo->m_strNotesName=Value;
																						pFolderInfo->m_wType=SUBROOM;
																						if(!theApp.stControl.m_bAdvanced)
																							pFolderInfo->b_Transfer=TRUE;

																						cobjNotesFolders.Add(pFolderInfo);
																						if(nPos>0)
																							ProcessSubRoomList(this,pFolderInfo->m_strNotesName,pFolderInfo->m_strFolderDisplayName.Left(nPos),cLastPart);
																						else
																							ProcessSubRoomList(this,pFolderInfo->m_strNotesName,pFolderInfo->m_strFolderDisplayName,cLastPart);
																					}
																				else
																					delete pFolderInfo; //No system name given, hence the item cannot be opened with that name
																		}
																	}
																Doc.Close();
															}
													}
												
											} while (nav.GotoNextMain(&entry) != LNWARN_NOT_FOUND);


									}
								folder.Close();   // Close the folder

							}
					}
				}
			else
				bRetValue=false;
		theApp.stControl.pDebugLog->LogOneLine("Exiting cQuickPlaceDB::Process()");
		theApp.stControl.pDebugLog->Flush();

		return bRetValue;


																																		

	}
